package channel;
import java.text.SimpleDateFormat;
import java.util.Date;
import spc.webos.data.IMessage;
import spc.webos.data.converter.XMLConverter2;
import spc.webos.endpoint.ESB2;
import spc.webos.endpoint.Endpoint;
import spc.webos.endpoint.EndpointFactory;
import spc.webos.endpoint.Executable;
import spc.webos.util.FileUtil;

public class TCPTest {




	public static void main(String[] args) throws Exception {
		// ESB 请求报文, 设置交易超时时间，单位秒
		//测试ESB心跳ESB.00000010.01
//		Executable exe = new Executable(XMLConverter2.getInstance().serialize(
//				readxml("ESB.00000010.01")), 30);
//		// 1, 如果ESB通过F5只暴漏一个IP地址
//		call("tcp://172.168.156.13:30012?hdrLen=8", exe);


		//测试金诚平台
		Executable exe = new Executable(XMLConverter2.getInstance().serialize(
				readxml("GCMS.0C8ACAA0.01")), 30);//30超时间间
		call("tcp://localhost:10122?hdrLen=8", exe);//金城地址


		// 2. 如果ESB没有使用F5， 暴漏2个集群的IP地址
		// call("tcp://219.143.38.252:30002,tcp://219.143.38.253:30002", exe);
	}

	public static IMessage readxml(String msgCd) throws Exception {
		byte[] xml = FileUtil.is2bytes(new TCPTest().getClass()
				.getResourceAsStream("../esbxml/" + msgCd + ".xml"));
		// 每次发送报文重新生成一个流水号， TCP/HTTP协议过FA，流水号不能重复
		IMessage msg = XMLConverter2.getInstance().deserialize(xml);
		String dt = new SimpleDateFormat("yyyyMMddHHmmssSSS")
				.format(new Date());
		msg.setSeqNb(ESB2.getInstance().getSeqNb(15));
		msg.setSndDt(dt.substring(0, 8));
		msg.setSndTm(dt.substring(8, 17));
		return msg;
	}

	static void call(String location, Executable exe) throws Exception {
		Endpoint endpoint = EndpointFactory.getInstance().getEndpoint(location);
		System.out.println("request::" + new String(exe.request, "utf-8"));
		endpoint.execute(exe);
		System.out.println("response::" + new String(exe.response, "utf-8"));
	}

	// static void farward() throws Exception {
	// Endpoint endpoint = EndpointFactory.getInstance().getEndpoint(
	// "tcp://www.sohu.com:80?hdrLen=0&http=true");
	// Executable exe = new Executable();
	// exe.request = "a".getBytes();
	// endpoint.execute(exe);
	// System.out.println("response::" + new String(exe.response, "utf-8"));
	// }

	static void testcache() {
		// Map2Cache cache = new Map2Cache();
		// cache.setExpireSeconds(1);
		// cache.setThreshold(1);
		// cache.put("a1", "a");
		// Thread.sleep(1200);
		// cache.put("a2", "a");
		// Thread.sleep(1200);
		// cache.put("a3", "a");
		// Thread.sleep(2000);
		// cache.put("a4", "a");
	}
}
